// pages/friend/friend.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')

Page({
  data: {
    headerUrl: "/images/person/defaultHeader.png",
    publisherPlatformId: "",
    name: '',
    guanzhuNum: 0,
    fensiNum: 0,
    attention: '+关注',
    buttonStatus: 'primary',
    cellList: [],
    page: 1,
  },
  onLoad(options) {
    this.setData({
      publisherPlatformId: options.userId
    });
    this.requestUserInfo();
    this.requestList();
  },
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    this.requestList();
    wx.stopPullDownRefresh();
  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    });
    this.requestList();
  },
  //头像加载错误处理
  headerImageLoadFail() {
    this.setData({
      headerUrl: "/images/person/defaultHeader.png"
    })
  },
  //点击关注 取消关注
  actionAttention() {
    if (this.data.attention == '取消关注') {
      let obj = {
        method: 'DELETE',
        contentType: 'application/x-www-form-urlencoded',
        url: '/user/attention/' + wx.getStorageSync('USERDATA').platformId + '/' + this.data.publisherPlatformId,
        showLoading: true,
        data: {
          fromPlatformId: wx.getStorageSync('USERDATA').platformId,
          targetPlatformId: this.data.publisherPlatformId
        }
      }
      httpUtils.request(obj).then(res => {
        ui.showToast("已取消");
        this.setData({
          attention: '+关注',
          buttonStatus: 'primary'
        })
      }).catch(err => {
        console.log('ERROR')
      });
    } else {
      let obj = {
        method: 'POST',
        contentType: 'application/x-www-form-urlencoded',
        url: '/user/attention/' + wx.getStorageSync('USERDATA').platformId + '/' + this.data.publisherPlatformId,
        showLoading: true,
        data: {
        }
      }
      httpUtils.request(obj).then(res => {
        ui.showToast("已关注");
        this.setData({
          attention: '取消关注',
          buttonStatus: 'default'
        })
      }).catch(err => {
        console.log('ERROR')
      });
    }
  },
  //加载个人信息
  requestUserInfo() {
    let obj = {
      method: 'GET',
      url: '/user/' + this.data.publisherPlatformId + '/task-user-info'
    }
    httpUtils.request(obj).then(res => {
      this.setData({
        headerUrl: res.data.avatar,
        name: res.data.nickName,
        guanzhuNum: res.data.attentionCount,
        fensiNum: res.data.fansCount
      })
      if (!res.data.attentioned) {
        this.setData({
          attention: '+关注',
          buttonStatus: 'primary'
        })
      } else {
        this.setData({
          attention: '取消关注',
          buttonStatus: 'default'
        })
      }
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //请求列表
requestList() {
  let obj = {
    method: 'GET',
    url: '/task',
    showLoading: true,
    data: {
      page: this.data.page,
      size: 20,
      publisherPlatformId: this.data.publisherPlatformId
    }
  }
  httpUtils.request(obj).then(res => {
    if (this.data.page == 1) {
      this.setData({
        cellList: []
      });
    }
    const {
      cellList = []
    } = this.data;
    res.data.taskListDetails.forEach(function (item, index) {
      cellList.push(item);
    });
    this.setData({
      cellList
    });
    
  }).catch(err => {
    console.log(err)
  });
},
  //任务详情
  actionTaskDetail(event) {
    wx.navigateTo({
      url: '/pages/task/taskDetail?taskNo=' + event.currentTarget.dataset.taskno,
    })
  },
})